package com.gsm.service.impl;

import com.gsm.dao.RotationChartDao;
import com.gsm.entity.Result;
import com.gsm.po.RotationChart;
import com.gsm.service.RotationChartService;
import com.gsm.utils.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 轮播图(RotationChart)表服务实现类
 */
@Service
public class RotationChartServiceImpl implements RotationChartService {
    @Autowired
    private RotationChartDao rotationChartDao;
    @Autowired
    private IdUtils idUtils;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Override
    public Result insertRotationChart(RotationChart rotationChart) {
        redisTemplate.delete("chart:list");
        Integer showOrder = rotationChartDao.selectMaxShowOrder();
        long chartId = idUtils.nextId();
        rotationChart.setChartId(chartId);
        rotationChart.setShowOrder(showOrder == null ? 0 : ++showOrder);
        rotationChartDao.insertRotationChart(rotationChart);
        return Result.success();
    }

    /**
     * 根据 主键 chartId 删除
     *
     * @param chartId 主键
     * @return
     */
    @Override
    public Result deleteRotationChartByChartId(Long chartId) {
        redisTemplate.delete("chart:list");
        rotationChartDao.deleteRotationChartByChartId(chartId);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param rotationChart 实例对象
     * @return
     */
    @Override
    public Result updateRotationChartByChartId(RotationChart rotationChart) {
        redisTemplate.delete("chart:list");
        rotationChartDao.updateRotationChartByChartId(rotationChart);
        return Result.success();
    }

    /**
     * 根据 id 查询单条数据
     *
     * @param chartId 主键
     * @return
     */
    @Override
    public Result selectRotationChartByChartId(Long chartId) {
        RotationChart rotationChart = rotationChartDao.selectRotationChartByChartId(chartId);
        Result result = Result.success(rotationChart);
        return result;
    }

    /**
     * 根据条件查询列表
     *
     * @param rotationChart 实例对象
     * @return
     */
    @Override
    public Result selectRotationChartList(RotationChart rotationChart) {
        List<RotationChart> list=null;
        ValueOperations<String, Object> ops = redisTemplate.opsForValue();
        list = (List<RotationChart>) ops.get("chart:list");
        if(list!=null){
            return Result.success(list);
        }
        list = rotationChartDao.selectRotationChartList(rotationChart);
        ops.set("chart:list",list);
        return Result.success(list);
    }

    @Override
    public Result updateChartShowOrder(List<Long> ids) {
        redisTemplate.delete("chart:list");
        rotationChartDao.updateChartShowOrder(ids);
        return Result.success();
    }
}